package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class Display extends JPanel{
	
	private static final long serialVersionUID = -5616028667891060319L;
	
	private Connection con;
	private JButton update = new JButton("Update");
		
	public Display(Connection con, String tableName) throws SQLException{
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		this.con = con;
						
		getTable(tableName);
		
	}
	
	private void getTable(String tableName) throws SQLException{
		
		this.removeAll();
		
		String[] columnNames = getColumnNames(tableName);
		
		Statement stmt = con.createStatement();
		ResultSet result = stmt.executeQuery("SELECT * FROM "+tableName);
		
		JTable table = rsToTable(result, columnNames);
		stmt.close();
		
		JScrollPane scrollPane = new JScrollPane(table);
				
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		update.addActionListener(new update(tableName));
		buttonPanel.add(update,BorderLayout.CENTER);
		
		add(scrollPane);
		add(buttonPanel);
	}
	
	private String[] getColumnNames(String tableName){
		
		String[] columnNames = null;
		
		if (tableName == "Item"){
			columnNames = new String[7];
			columnNames[0] = "upc";
			columnNames[1] = "title";
			columnNames[2] = "type";
			columnNames[3] = "category";
			columnNames[4] = "company";
			columnNames[5] = "year";
			columnNames[6] = "sellPrice";
			
		}
		if (tableName == "LeadSinger"){
			columnNames = new String[2];
			columnNames[0] = "upc";
			columnNames[1] = "name";
		}
		if (tableName == "HasSong"){
			columnNames = new String[2];
			columnNames[0] = "upc";
			columnNames[1] = "title";
		}
		if (tableName == "Supplier"){
			columnNames = new String[4];
			columnNames[0] = "name";
			columnNames[1] = "address";
			columnNames[2] = "city";
			columnNames[3] = "status";
		}
		if (tableName == "Shipment"){
			columnNames = new String[4];
			columnNames[0] = "sid";
			columnNames[1] = "supName";
			columnNames[2] = "storeName";
			columnNames[3] = "shipDate";
		}
		if (tableName == "ShipItem"){
			columnNames = new String[4];
			columnNames[0] = "sid";
			columnNames[1] = "upc";
			columnNames[2] = "supPrice";
			columnNames[3] = "quantity";
		}
		if (tableName == "Store"){
			columnNames = new String[3];
			columnNames[0] = "name";
			columnNames[1] = "address";
			columnNames[2] = "type";
		}
		if (tableName == "Stored"){
			columnNames = new String[3];
			columnNames[0] = "name";
			columnNames[1] = "upc";
			columnNames[2] = "stock";
		}
		if (tableName == "Purchase"){
			columnNames = new String[8];
			columnNames[0] = "receiptID";
			columnNames[1] = "purchaseDate";
			columnNames[2] = "cid";
			columnNames[3] = "name";
			columnNames[4] = "card#";
			columnNames[5] = "expire";
			columnNames[6] = "expectedDate";
			columnNames[7] = "deliveredDate";
		}
		if (tableName == "PurchaseItem"){
			columnNames = new String[3];
			columnNames[0] = "receiptID";
			columnNames[1] = "upc";
			columnNames[2] = "quantity";
		}
		if (tableName == "Customer"){
			columnNames = new String[5];
			columnNames[0] = "cid";
			columnNames[1] = "password";
			columnNames[2] = "name";
			columnNames[3] = "address";
			columnNames[4] = "phone";
		}
		if (tableName == "Return"){
			columnNames = new String[4];
			columnNames[0] = "retid";
			columnNames[1] = "returnDate";
			columnNames[2] = "receiptId";
			columnNames[3] = "name";
		}
		if (tableName == "ReturnItem"){
			columnNames = new String[3];
			columnNames[0] = "retid";
			columnNames[1] = "upc";
			columnNames[2] = "quantity";
		}
		
		return columnNames;
	}	
		
	// Returns a 2-D Object Array from the ResultSet.
	public static JTable rsToTable(ResultSet result, String[] columnNames) throws SQLException{
		List<Object[]> rows = new ArrayList<Object[]>();
		
		while (result.next()){
			Object[] row = new Object[columnNames.length+1];
			
			for (int i=0; i<columnNames.length; i++){
				Object value = result.getObject(columnNames[i]);
				row[i] = value;
			}			
			rows.add(row);
		}
		
		Object[][] data = new Object[rows.size()][columnNames.length+1];
		
		for (int i=0; i<rows.size(); i++){
			data[i] = rows.get(i);
		}
		
		JTable table = new JTable(data, columnNames);
		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);
		
		return table;
	}
	
	public class update implements ActionListener{
		
		String tableName;
		
		public update(String tableName) {
			this.tableName = tableName;
		}

		public void actionPerformed(ActionEvent e){

			try{
				getTable(tableName);
			} catch (SQLException msg){
				System.out.print(msg);
			}
		}
	}
}